package cn.hongwei.eduservice.service.impl;

import cn.hongwei.eduservice.entity.Course;
import cn.hongwei.eduservice.entity.CourseDescription;
import cn.hongwei.eduservice.entity.form.CourseInfoForm;
import cn.hongwei.eduservice.entity.query.CourseApiQuery;
import cn.hongwei.eduservice.entity.vo.CourseDetailsVo;
import cn.hongwei.eduservice.entity.vo.CourseInfoVo;
import cn.hongwei.eduservice.mapper.CourseDescriptionMapper;
import cn.hongwei.eduservice.mapper.CourseMapper;
import cn.hongwei.eduservice.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author Great
 * @email 1224028994@qq.com
 */
@Service
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseMapper,Course> implements CourseService {
    @Autowired//课程信息的mapper
    private CourseDescriptionMapper descriptionMapper;

    @Transactional
    @Override
    public String saveCourseInfo(CourseInfoForm courseInfo) {
        /*
        * 创建一个课程对象
        * 然后把前端页面表单中的数据拷贝到我们创建的课程对象中
        * 将我们创建的课程对象插入到课程表格(eud_course)
        *
        * 然后就是要生成对应的课程描述信息的对象了:
        *   创建课程描述信息对象
        *   将前台传递过来的表单数据获取描述信息设置给创建的对象
        *   获取新增的课程记录id
        *   设置描述信息对象的id为新增课程的id(因为课程跟课程描述是有对应关系的)
        *   将课程描述信息对象插入到对应的表中(edu_course_description)
        * */
        Course course=new Course();
        BeanUtils.copyProperties(courseInfo,course);//新知识
        baseMapper.insert(course);

        //先获取新增课程的id
        String courseId = course.getId();

        CourseDescription courseDescription=new CourseDescription();
        courseDescription.setDescription(courseInfo.getDescription());
        courseDescription.setId(courseId);
        //插入到对应的表中
        descriptionMapper.insert(courseDescription);

        //返回新增的课程id
        return courseId;
    }

    @Override
    public Course getById(String id) {
        return baseMapper.selectById(id);
    }

    //分页查询讲师和发布的课程
    @Override
    public Page<CourseInfoVo> getCourseInfoVos(Integer pageNum, Integer pageSize) {
        Page<CourseInfoVo> page = new Page(pageNum , pageSize);
        //查询 Page需要的分页数据集合
        // limit  index, pageSize     total:总记录条数
        // mp: 只需要在自定义的mapper的方法中  传入一个page对象，携带pageNUm和pageSize参数值，mp会自动使用分页拦截器使用它的条件
        QueryWrapper<CourseInfoVo> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("1" , "1");
        queryWrapper.orderByDesc("t1.gmt_create");
        List<CourseInfoVo> courseInfoVos = baseMapper.getCourseInfoVos(page,queryWrapper);

        page.setRecords(courseInfoVos);//将查询到的分页数据集合设置给page对象
        return page;
    }

    @Override
    public CourseInfoVo getPublishCourse(String courseId) {
        //由于以后多表联查自定义sql 涉及的字段从多表中获取，不能使用LambdaQueryWrapper 自动分析javabean的属性名映射的表的字段名
        QueryWrapper<CourseInfoVo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("t1.id" , courseId);  //根据课程id查询课程详情
        return baseMapper.getCourseInfoVoByCourseId(queryWrapper);
    }

    //带条件查询课程列表
    @Override
    public List<Course> listCourses(CourseApiQuery courseApiQuery) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        String subjectId = courseApiQuery.getSubjectId();
        String subjectParentId = courseApiQuery.getSubjectParentId();
        Integer type = courseApiQuery.getType();
        String orderByColumn = courseApiQuery.getOrderByColumn();
        //用户只能访问已发布的课程
//        queryWrapper.eq("status" , "Normal");

        if(!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("subject_parent_id" , subjectParentId);
        }
        if(!StringUtils.isEmpty(subjectId)){//筛选条件
            queryWrapper.eq("subject_id" , subjectId);
        }
        if(!StringUtils.isEmpty(orderByColumn)){//排序字段
            if(type==null || type==0){
                //orderByColumn 传入的值必须和数据库要求排序的列名一样
                queryWrapper.orderByDesc(orderByColumn);

            }else{
                queryWrapper.orderByAsc(orderByColumn);

            }

        }
        return baseMapper.selectList(queryWrapper);
    }

    //获取课程详情
    @Override
    public CourseDetailsVo getCourseDetails(String courseId) {
        return baseMapper.getCourseDetails(courseId);
    }
}
